package com.heifeng.cloudshelf.mvp.programme.adapter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.event.DialogSpecTextEvent;
import com.heifeng.cloudshelf.mvp.model.ProgrammeSelectInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by admin on 2019/9/19.
 */

public class DialogSpecTextRvAdapter extends BaseQuickAdapter<ProgrammeSelectInfo.OptionBean, BaseViewHolder> {
    private List<ProgrammeSelectInfo.OptionBean> data;

    private List<ProgrammeSelectInfo> infos;

    private static OnSpecChangedListener onSpecChangedListener;

    public static void setOnSpecChangedListener(OnSpecChangedListener onSpecChangedListener) {
        DialogSpecTextRvAdapter.onSpecChangedListener = onSpecChangedListener;
    }

    private int selectPosition = 0;

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public DialogSpecTextRvAdapter(Context context, int layoutResId, @Nullable List<ProgrammeSelectInfo.OptionBean> data, List<ProgrammeSelectInfo> infos) {
        super(layoutResId, data);
        mContext = context;
        this.data = data;
        this.infos = infos;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, ProgrammeSelectInfo.OptionBean item) {
        if (item.isSelected()) {
            helper.getView(R.id.l_string).setBackground(mContext.getResources().getDrawable(R.mipmap.ic_chosen_sel));
        } else {
            helper.getView(R.id.l_string).setBackground(mContext.getResources().getDrawable(R.mipmap.ic_unchosen_sel));
        }
//        if (helper.getPosition() == selectPosition) {
//            helper.getView(R.id.l_string).setBackground(mContext.getResources().getDrawable(R.mipmap.ic_chosen_sel));
//        } else {
//            helper.getView(R.id.l_string).setBackground(mContext.getResources().getDrawable(R.mipmap.ic_unchosen_sel));
//        }

        helper.getView(R.id.iv_more).setVisibility(View.GONE);

        helper.setText(R.id.tv_text, item.getSpec_name());

        helper.getView(R.id.l_string).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!item.isSelected()) {
                    for (int i = 0; i < getData().size(); i++) {
                        if (getData().get(i).isSelected()) {
                            getData().get(i).setSelected(false);
                        }
                    }
                    item.setSelected(true);
                    setSelectPosition(helper.getPosition());
                    notifyDataSetChanged();
                    if (null != onSpecChangedListener) {
                        onSpecChangedListener.onChanged(item.getId(), item.getParentId(), item.getGroupId());
                    }
                    EventBus.getDefault().post(new DialogSpecTextEvent(item.getGroupId(), helper.getPosition(), item.getSpec_name(), item.getId()));
                }

            }
        });
    }

    public interface OnSpecChangedListener {
        void onChanged(int specTextId, int parentId, int index);
    }
}
